package com.marvels.financial.loan.api.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.api.IProductInfoApi;
import com.marvels.financial.loan.dto.McptProduct;
import com.marvels.product.param.service.AmortMethodService;
import com.marvels.product.param.service.ConsultingFeeService;
import com.marvels.product.param.service.LateFeeService;
import com.marvels.product.param.service.McptPaymentSequenceItemService;
import com.marvels.product.param.service.MiscFeeService;
import com.marvels.product.param.service.MngFeeService;
import com.marvels.product.param.service.PaymentSequenceService;
import com.marvels.product.param.service.PenaltyFeeService;
import com.marvels.product.param.service.PlatformFeeService;
import com.marvels.product.param.service.PreTerminateFeeService;
import com.marvels.product.param.service.ProductItemService;
import com.marvels.product.productmanage.service.ProductTypeService;

import cn.hutool.core.bean.BeanUtil;

@Service
public class IProductInfoApiImpl implements IProductInfoApi {

	@Autowired
	private ProductItemService itemService;
	
	@Autowired
	private AmortMethodService amortMethodService;
	
	@Autowired
	private ConsultingFeeService consultingFeeService;
	
	@Autowired
	private LateFeeService lateFeeService;
	
	@Autowired
	private MngFeeService mngFeeService;
	
	@Autowired
	private MiscFeeService miscFeeService;
	
	@Autowired
	private PaymentSequenceService sequenceService;
	
	@Autowired
	private PenaltyFeeService penaltyFeeService;
	
	@Autowired
	private PlatformFeeService platformFeeService;
	
	@Autowired
	private PreTerminateFeeService preTerminateFeeService;
	
	@Autowired
	private ProductTypeService productTypeService;
	
	@Autowired
	private McptPaymentSequenceItemService mcptPaymentSequenceItemService;
	
	@Override
	public MarvelsResultDTO queryByProductCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptProduct mcptProduct = itemService.getByProductCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryByProductCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptProduct);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryByProductCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryAmortMethodByAmortMethodCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptAmortMethodDTO mcptAmortMethodDTO = amortMethodService.getAmortMethodByAmortMethodCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryAmortMethodByAmortMethodCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptAmortMethodDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryAmortMethodByAmortMethodCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryConsultingFeeByConsultingFeeCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptConsultingFeeDTO mcptConsultingFeeDTO = consultingFeeService.getConsultingFeeByConsultingFeeCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryConsultingFeeByConsultingFeeCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),mcptConsultingFeeDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryConsultingFeeByConsultingFeeCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryLateFeeByLateFeeCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptLateFeeDTO mcptLateFeeDTO = lateFeeService.getLateFeeByLateFeeCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryLateFeeByLateFeeCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptLateFeeDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryLateFeeByLateFeeCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryManagementFeeByManageFeeCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptManagementFeeDTO mcptManagementFeeDTO = mngFeeService.getManagementFeeByManageFeeCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryManagementFeeByManageFeeCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptManagementFeeDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryManagementFeeByManageFeeCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryMiscFeeByMiscFeeCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptMiscFeeDTO mcptMiscFeeDTO = miscFeeService.getMiscFeeByMiscFeeCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryMiscFeeByMiscFeeCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptMiscFeeDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryMiscFeeByMiscFeeCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryPaymentSequenceByPaymentSeq(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptPaymentSequenceDTO mcptPaymentSequence = sequenceService.getPaymentSequenceByPaymentSeq(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryPaymentSequenceByPaymentSeq direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptPaymentSequence);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryPaymentSequenceByPaymentSeq error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryPenaltyFeeByPenaltyFeeCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptPenaltyFeeDTO mcptPenaltyFeeDTO = penaltyFeeService.getPenaltyFeeByPenaltyFeeCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryPenaltyFeeByPenaltyFeeCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptPenaltyFeeDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryPenaltyFeeByPenaltyFeeCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryPlatformFeeByPlatformFeeCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptPlatformFeeDTO mcptPlatformFeeDTO = platformFeeService.getPlatformFeeByPlatformFeeCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryPlatformFeeByPlatformFeeCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptPlatformFeeDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryPlatformFeeByPlatformFeeCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryPreTerminateFeeByPreTerminateFeeCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			com.marvels.product.param.dto.McptPreTerminationFeeDTO mcptPreTerminationFeeDTO = preTerminateFeeService.getPreTerminateFeeByPreTerminateFeeCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryPreTerminateFeeByPreTerminateFeeCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptPreTerminationFeeDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryPreTerminateFeeByPreTerminateFeeCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryRateByProductCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			List<com.marvels.product.productmanage.dto.McptProductFundRelationDto> mcptProductFundRelationDto = productTypeService.getRateByProductCode(param);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryRateByProductCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), mcptProductFundRelationDto);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryRateByProductCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryFeeRateByProductCode(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			MarvelsLogUtil.getInstance().info("the method queryFeeRateByProductCode ===" + JSONObject.toJSONString(param));
			//产品映射资金源
			List<com.marvels.product.productmanage.dto.McptProductFundRelationDto> mcptProductFundRelationDto = productTypeService.getRateByProductCode(param);
			//产品小类信息
			com.marvels.product.param.dto.McptProduct mcptProduct = itemService.getByProductCode(param);
			//还款方式
			com.marvels.product.param.dto.McptAmortMethodDTO mcptAmortMethodDTO =amortMethodService.getAmortMethodByAmortMethodCode(mcptProduct);
			//咨询费
			com.marvels.product.param.dto.McptConsultingFeeDTO mcptConsultingFeeDTO = consultingFeeService.getConsultingFeeByConsultingFeeCode(mcptProduct);
			MarvelsLogUtil.getInstance().info(JSONObject.toJSONString(mcptProduct));
			//管理费
			com.marvels.product.param.dto.McptManagementFeeDTO mcptManagementFeeDTO = mngFeeService.getManagementFeeByManageFeeCode(mcptProduct);
			//平台费
			com.marvels.product.param.dto.McptPlatformFeeDTO mcptPlatformFeeDTO = platformFeeService.getPlatformFeeByPlatformFeeCode(mcptProduct);
			//杂费
			com.marvels.product.param.dto.McptMiscFeeDTO mcptMiscFeeDTO = miscFeeService.getMiscFeeByMiscFeeCode(mcptProduct);
			//违约金
			com.marvels.product.param.dto.McptPenaltyFeeDTO mcptPenaltyFeeDTO = penaltyFeeService.getPenaltyFeeByPenaltyFeeCode(mcptProduct);
			//滞纳金
			com.marvels.product.param.dto.McptLateFeeDTO mcptLateFeeDTO = lateFeeService.getLateFeeByLateFeeCode(mcptProduct);
			//提前结清费信息
			com.marvels.product.param.dto.McptPreTerminationFeeDTO mcptPreTerminationFeeDTO = preTerminateFeeService.getPreTerminateFeeByPreTerminateFeeCode(mcptProduct);
			//信息集合dto
			com.marvels.product.param.dto.ProductMsgInfoDTO productMsgInfoDTO = new com.marvels.product.param.dto.ProductMsgInfoDTO();
			productMsgInfoDTO.setMcptProduct(mcptProduct);
			productMsgInfoDTO.setMcptAmortMethodDTO(mcptAmortMethodDTO);
			productMsgInfoDTO.setMcptConsultingFeeDTO(mcptConsultingFeeDTO);
			productMsgInfoDTO.setMcptManagementFeeDTO(mcptManagementFeeDTO);
			productMsgInfoDTO.setMcptPlatformFeeDTO(mcptPlatformFeeDTO);
			productMsgInfoDTO.setMcptMiscFeeDTO(mcptMiscFeeDTO);
			productMsgInfoDTO.setMcptProductFundRelationDto(mcptProductFundRelationDto);
			productMsgInfoDTO.setMcptPreTerminationFeeDTO(mcptPreTerminationFeeDTO);
			productMsgInfoDTO.setMcptPenaltyFeeDTO(mcptPenaltyFeeDTO);
			productMsgInfoDTO.setMcptLateFeeDTO(mcptLateFeeDTO);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryFeeRateByProductCode direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), productMsgInfoDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryFeeRateByProductCode error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryPrePayTrailMsg(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			MarvelsLogUtil.getInstance().info("the method queryPrePayTrailMsg ===" + JSONObject.toJSONString(param));
			//产品小类信息
			com.marvels.product.param.dto.McptProduct mcptProduct=itemService.getByProductCode(param);
			//还款方式
			com.marvels.product.param.dto.McptAmortMethodDTO mcptAmortMethodDTO =amortMethodService.getAmortMethodByAmortMethodCode(mcptProduct);
			//提前结清费信息
			com.marvels.product.param.dto.McptPreTerminationFeeDTO mcptPreTerminationFeeDTO = preTerminateFeeService.getPreTerminateFeeByPreTerminateFeeCode(mcptProduct);
			//信息集合dto
			com.marvels.product.param.dto.ProductMsgInfoDTO productMsgInfoDTO = new com.marvels.product.param.dto.ProductMsgInfoDTO();
			productMsgInfoDTO.setMcptProduct(mcptProduct);
			productMsgInfoDTO.setMcptAmortMethodDTO(mcptAmortMethodDTO);
			productMsgInfoDTO.setMcptPreTerminationFeeDTO(mcptPreTerminationFeeDTO);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryPrePayTrailMsg direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), productMsgInfoDTO);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryPrePayTrailMsg error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryPaymentSequenceItemByPaymentSeq(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			MarvelsLogUtil.getInstance().info("the method queryPaymentSequenceByPaymentSeq ===" + JSONObject.toJSONString(dto));
			List<com.marvels.product.param.dto.McptPaymentSequenceItemDTO> list= mcptPaymentSequenceItemService.getPaymentSequenceItemBySequenceCode(param);
			com.marvels.product.param.dto.ProductMsgInfoDTO msgDto = new com.marvels.product.param.dto.ProductMsgInfoDTO();
			msgDto.setMcptPaymentSequenceItemDTO(list);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryPaymentSequenceItemByPaymentSeq direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), msgDto);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryPaymentSequenceItemByPaymentSeq error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

	@Override
	public MarvelsResultDTO queryStrikeBalanceMsg(McptProduct dto) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(dto, param);
			MarvelsLogUtil.getInstance().info("the method queryPrePayTrailMsg ===" + JSONObject.toJSONString(dto));
			//产品小类信息
			com.marvels.product.param.dto.McptProduct mcptProduct=itemService.getByProductCode(param);
			//还款方式
			com.marvels.product.param.dto.McptAmortMethodDTO mcptAmortMethodDTO =amortMethodService.getAmortMethodByAmortMethodCode(mcptProduct);
			//提前结清费信息
			com.marvels.product.param.dto.McptPreTerminationFeeDTO mcptPreTerminationFeeDTO = preTerminateFeeService.getPreTerminateFeeByPreTerminateFeeCode(mcptProduct);
			//还款顺序
			MarvelsLogUtil.getInstance().info("the method mcptProduct ===" + JSONObject.toJSONString(mcptProduct));
			List<com.marvels.product.param.dto.McptPaymentSequenceItemDTO> list= mcptPaymentSequenceItemService.getPaymentSequenceItemBySequenceCode(mcptProduct);
			//信息集合dto
			com.marvels.product.param.dto.ProductMsgInfoDTO productMsgInfoDTO = new com.marvels.product.param.dto.ProductMsgInfoDTO();
			productMsgInfoDTO.setMcptProduct(mcptProduct);
			productMsgInfoDTO.setMcptPaymentSequenceItemDTO(list);
			productMsgInfoDTO.setMcptAmortMethodDTO(mcptAmortMethodDTO);
			productMsgInfoDTO.setMcptPreTerminationFeeDTO(mcptPreTerminationFeeDTO);
			MarvelsLogUtil.getInstance().info("IProductInfoApi.queryStrikeBalanceMsg direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), productMsgInfoDTO);
		}  catch (Exception e) {
			MarvelsLogUtil.getInstance().error("IProductInfoApi.queryStrikeBalanceMsg error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),"系统异常");
		}
	}

}
